<?php
namespace Admin\Controller;

use Think\Controller;

class VirtualcurrencyController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_virtualcurrency");
			$params = session("QG_SEARCH_PARAMS_virtualcurrency");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_virtualcurrency");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_virtualcurrency", null);
            session("QG_SEARCH_PARAMS_virtualcurrency", null);
            session("QG_SEARCH_SIZE_virtualcurrency", null);
            $pageSize = I("post.pageSize");
			$params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }
		
		// 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('virtualcurrency')
            ->alias("t")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('virtualcurrency')
            ->alias("t")
            ->join("left join member m on m.id=t.userId")
            ->field("t.*,m.nickname username")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();
			
		// 统计
        $sum = M('virtualcurrency')
            ->alias("t")
            ->field("count(1) as countNum")
            ->where($where)
            ->order('t.id DESC')
            ->find();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_virtualcurrency", $where);
		session("QG_SEARCH_PARAMS_virtualcurrency", $params);
		session("QG_SEARCH_SIZE_virtualcurrency", $pageSize);
		
		// 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('sum', $sum);
		$this->assign('lists', array(
            "page" => getDropdownList("pageSize" ,"" ,false)
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 
        if (I('post.id')) {
            $where .= " AND t.id='" . I("post.id") . "'";
        }

        // 账号
        if (I('post.account')) {
            // 精确
            if (I('post.accountCondition') == 1) {
                $where .= " AND t.account='" . I("post.account") . "'";
            } else {
                $where .= " AND t.account LIKE '%" . I("post.account") . "%'";
            }
        }

        // 对应用户表id
        if (I('post.userid')) {
            $where .= " AND t.userid='" . I("post.userid") . "'";
        }

        // vpay
        if (I('post.vpayStart') || I('post.vpayEnd')) {
            // 开始
            if(I('post.vpayStart')){
                $where .= " AND t.vpay>='" . I("post.vpayStart") . "'";
            }
            // 结束
            if(I('post.vpayEnd')){
                $where .= " AND t.vpay<='" . I("post.vpayEnd") . "'";
            }
        }

        // 比特币
        if (I('post.bitcoinStart') || I('post.bitcoinEnd')) {
            // 开始
            if(I('post.bitcoinStart')){
                $where .= " AND t.bitcoin>='" . I("post.bitcoinStart") . "'";
            }
            // 结束
            if(I('post.bitcoinEnd')){
                $where .= " AND t.bitcoin<='" . I("post.bitcoinEnd") . "'";
            }
        }

        // 莱特币
        if (I('post.litecoinStart') || I('post.litecoinEnd')) {
            // 开始
            if(I('post.litecoinStart')){
                $where .= " AND t.litecoin>='" . I("post.litecoinStart") . "'";
            }
            // 结束
            if(I('post.litecoinEnd')){
                $where .= " AND t.litecoin<='" . I("post.litecoinEnd") . "'";
            }
        }

        // 以太坊
        if (I('post.ethereumStart') || I('post.ethereumEnd')) {
            // 开始
            if(I('post.ethereumStart')){
                $where .= " AND t.ethereum>='" . I("post.ethereumStart") . "'";
            }
            // 结束
            if(I('post.ethereumEnd')){
                $where .= " AND t.ethereum<='" . I("post.ethereumEnd") . "'";
            }
        }

        // 狗狗币
        if (I('post.dogecoinStart') || I('post.dogecoinEnd')) {
            // 开始
            if(I('post.dogecoinStart')){
                $where .= " AND t.dogecoin>='" . I("post.dogecoinStart") . "'";
            }
            // 结束
            if(I('post.dogecoinEnd')){
                $where .= " AND t.dogecoin<='" . I("post.dogecoinEnd") . "'";
            }
        }

        return $where;
    }

    // 添加会员
    public function add()
    {
        if (IS_POST) {
            // 数据
            $data = array();

                // 
                    $data['id'] = I('post.id');
                // 账号
                    $data['account'] = I('post.account');
                // 对应用户表id
                    $data['userId'] = I('post.userid');
                // vpay
                    $data['vpay'] = I('post.vpay');
                // 比特币
                    $data['bitcoin'] = I('post.bitcoin');
                // 莱特币
                    $data['litecoin'] = I('post.litecoin');
                // 以太坊
                    $data['ethereum'] = I('post.ethereum');
                // 狗狗币
                    $data['dogecoin'] = I('post.dogecoin');

            //创建数据
            $result = M('virtualcurrency')->add($data);

            if ($result) {
				$this->ajaxSuccess("添加成功！");
            } else {
				$this->ajaxError("添加失败！");
            }
        } else {
            $this->display();
        }
    }

    // 编辑资料
    public function edit()
    {
        if (IS_POST) {
            $return = array();
            // ID
            $id = I("post.id");
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}
            // 判断用户是否存在
            $dbData = M('virtualcurrency')
                ->where(array("id", $id))
                ->find();

            //判断用户是否存在
            if (empty($dbData)) {
				$this->ajaxError("数据不存在！");
            }

            // 更新数据
            $data = array();
            // vpay
            $data['vpay'] = I('post.vpay');
            // 比特币
            $data['bitcoin'] = I('post.bitcoin');
            // 莱特币
            $data['litecoin'] = I('post.litecoin');
            // 以太坊
            $data['ethereum'] = I('post.ethereum');
            // 狗狗币
            $data['dogecoin'] = I('post.dogecoin');

            // 更新数据
            $result = M('virtualcurrency')
                ->where(array("id" => $id))
                ->save($data);

            if ($result !== false) {
				$this->ajaxSuccess("更新成功！");
            } else {
				$this->ajaxError("更新失败！");
            }
        } else {
            // ID
            $id = intval(I("get.id"));
            // 判断是否存在
            $data = M('virtualcurrency')
                ->alias("t")
                ->join("left join member m on m.id=t.userId")
                ->field("t.*,m.nickname")
                ->where(array("t.id" => $id))
                ->find();

            // 判断是否存在
            if (empty($data)) {
                $this->error("数据不存在");
            }

            $this->assign("data", $data);
            $this->display();
        }
    }

    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}

            //删除数据
            $result = M('virtualcurrency')->where(array("id" => $id))->delete();

            // 如果删除成功
            if ($result) {
				$this->ajaxSuccess("删除成功！");
            } else {
				$this->ajaxError("删除失败！");
            }
        }
    }
}